/*
* Author: Sowmya Kameswaran
*/
package com.foo.RealTimeData;

import java.sql.Timestamp;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.foo.Entity.RealTimeData;
import com.foo.Entity.TickerInfoData;
import com.foo.TextDataSender.DataSender;


@Stateless(name="RealTimeDataProvider")
public class RealTimeDataProvider implements RealTimeDataProviderRemote, RealTimeDataProviderLocal 
{
	@PersistenceContext(unitName="realtimeinformationprovider") // This is the entity manager required by the Entity bean
	private EntityManager em;

	int total =0 ;
	List<RealTimeData> realtimedatalist = null;
	Query getrealtimedata = null;
	Query setlastupdate = null;
	Query gettickerdata = null;
	List<TickerInfoData> tickerdata = null;
	String realtimemessage="";
	DataSender datasender = new DataSender();
	//java.util.Date s1=new java.util.Date();	
	//java.sql.Date dateval=new java.sql.Date(s1.getTime());
    public RealTimeDataProvider() 
    {
		/*try
		{
			Properties properties = new Properties();
			properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
			properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
			properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
			Context ctx = new InitialContext(properties);
			Object ref = ctx.lookup("DataSender/remote");
			datasender = (DataSenderRemote) PortableRemoteObject.narrow(ref, DataSenderRemote.class);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
*/
		System.out.println("In server class to retrieve data \n"); 
	}
    
    @Override
	public void Realtimeinfo(String param, Timestamp timestamp) 
	{
    
		System.out.println("Entering server function Realtime Data with parameters " +param + "," + timestamp);
		realtimemessage="";
		getrealtimedata=em.createQuery("from RealTimeData d where ticker_symbol=:ticker_symbol order by d.currentdate"); //idea was t get the one updated the earliest
		getrealtimedata.setParameter("ticker_symbol", param);
		getrealtimedata.setMaxResults(1);
		realtimedatalist = getrealtimedata.getResultList();

		setlastupdate = em.createQuery("UPDATE RealTimeData dd SET dd.currentdate =:currentdate where dd.ticker_symbol=:ticker_symbol and id=:id");
		setlastupdate.setParameter("currentdate", timestamp);
		setlastupdate.setParameter("ticker_symbol", param);
		setlastupdate.setParameter("id",realtimedatalist.get(0).getId());
		int updated = setlastupdate.executeUpdate ();

		if(1 == updated)
		{
		System.out.println("Update Date Success ");
		}
		else
		{
		System.out.println("Update Date Failed ");

		}
		gettickerdata = em.createQuery("from TickerInfoData d where d.ticker_symbol = :ticker_symbol");
		gettickerdata.setParameter("ticker_symbol", param);
		gettickerdata.setMaxResults(1);
		tickerdata=gettickerdata.getResultList();
		
		int listsize = realtimedatalist.size();
		System.out.println("list size "+listsize);
		for(int k=0;k<listsize;k++)
		{
			System.out.println("Inside for loop of realtime data \n");
			realtimemessage += realtimedatalist.get(k).getTicker_symbol() + "," +realtimedatalist.get(k).getOpen_value()+"," +timestamp+ ',' + tickerdata.get(k).getCompany() 
			+ ',' + tickerdata.get(k).getSector_name() + ',' + tickerdata.get(k).getIndustry() + ',' + tickerdata.get(k).getCountry() + ',' + tickerdata.get(k).getState() 
			+ ',' + tickerdata.get(k).getMarket_cap() + ',';
		}
	
		System.out.println("The message " +realtimemessage);
		datasender.sendMessage("RealTime",realtimemessage);
		
	}
}
